package file

import (
	"os"
	"path"
)

func FileExists(absolutelyPath string) bool {
	_, err := os.Stat(absolutelyPath)
	return err == nil || os.IsExist(err)
}

func CreateFile(absolutelyPath string) (*os.File, error) {
	errDir := os.MkdirAll(path.Dir(absolutelyPath), 0777)
	if errDir != nil {
		return nil, errDir
	} else {
		if file, errFile := os.Create(absolutelyPath); errFile == nil {
			return file, nil
		} else {
			return nil, errFile
		}
	}
	//}
}

func CreateIfNotExists(absolutelyPath string) (*os.File, error) {
	if !FileExists(absolutelyPath) {
		return CreateFile(absolutelyPath)
	} else {
		return os.Open(absolutelyPath)
	}
}

func DeleteFilePath(absolutelyPath string) error {
	if !FileExists(absolutelyPath) {
		return nil
	} else {
		return os.RemoveAll(absolutelyPath)
	}
}
